package com.jhsw.roadsun.module.business.controller.app.useraddress;

import com.jhsw.roadsun.framework.common.pojo.CommonResult;
import com.jhsw.roadsun.framework.common.pojo.PageResult;
import com.jhsw.roadsun.framework.common.util.object.BeanUtils;
import com.jhsw.roadsun.framework.security.core.util.SecurityFrameworkUtils;
import com.jhsw.roadsun.module.business.controller.app.useraddress.vo.AppUserAddressPageReqVO;
import com.jhsw.roadsun.module.business.controller.app.useraddress.vo.AppUserAddressRespVO;
import com.jhsw.roadsun.module.business.controller.app.useraddress.vo.UserAddressSaveReqVO;
import com.jhsw.roadsun.module.business.dal.dataobject.useraddress.UserAddressDO;
import com.jhsw.roadsun.module.business.service.useraddress.UserAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.jhsw.roadsun.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 地址簿")
@RestController
@RequestMapping("/biz/user-address")
@Validated
public class AppUserAddressController {

    @Resource
    private UserAddressService userAddressService;

    @PostMapping("/create")
    @Operation(summary = "创建地址簿")
    public CommonResult<Long> createUserAddress(@Valid @RequestBody UserAddressSaveReqVO createReqVO) {
        return success(userAddressService.createUserAddress(createReqVO, SecurityFrameworkUtils.getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新地址簿")
    public CommonResult<Boolean> updateUserAddress(@Valid @RequestBody UserAddressSaveReqVO updateReqVO) {
        userAddressService.updateUserAddress(updateReqVO, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除地址簿")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteUserAddress(@RequestParam("id") Long id) {
        userAddressService.appDeleteUserAddress(id, SecurityFrameworkUtils.getLoginUserId());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取地址簿详情")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<AppUserAddressRespVO> getUserAddress(@RequestParam("id") Long id) {
        UserAddressDO userAddress = userAddressService.getUserAddress(id);
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return success(userAddress.getUserId().equals(loginUserId) ? BeanUtils.toBean(userAddress, AppUserAddressRespVO.class) : null);
    }

    @GetMapping("/get/normal")
    @Operation(summary = "获取默认地址簿详情")
    @Parameter(name = "type", description = "类型（0寄件地址 1收件地址）", required = true)
    public CommonResult<AppUserAddressRespVO> getUserNormalAddress(@RequestParam("type") Integer type) {
        return success(userAddressService.getUserNormalAddress(type, SecurityFrameworkUtils.getLoginUserId()));
    }

    @GetMapping("/page")
    @Operation(summary = "获得地址簿分页")
    public CommonResult<PageResult<AppUserAddressRespVO>> getUserAddressPage(@Valid AppUserAddressPageReqVO pageReqVO) {
        return success(userAddressService.appGetUserAddressPage(pageReqVO, SecurityFrameworkUtils.getLoginUserId()));
    }
}